Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.3, n = 245)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.53689 11.57461 11.61204 11.64918 11.68601 11.72255 11.75880 11.79475
##   [9] 11.83041 11.86579 11.90087 11.93567 11.97019 12.00438 12.03825 12.07181
##  [17] 12.10508 12.13808 12.17083 12.20336 12.23567 12.26775 12.29959 12.33117
##  [25] 12.36248 12.39351 12.42424 12.45466 12.48475 12.51451 12.54391 12.57294
##  [33] 12.60159 12.62985 12.65857 12.68842 12.71910 12.75028 12.78167 12.81293
##  [41] 12.84377 12.87387 12.90292 12.93061 12.95662 12.98065 13.00238 13.02150
##  [49] 13.03950 13.05785 13.07623 13.09432 13.11178 13.12831 13.14357 13.15723
##  [57] 13.16898 13.17849 13.18543 13.18948 13.19032 13.18761 13.18068 13.16951
##  [65] 13.15481 13.13727 13.11758 13.09644 13.07456 13.05262 13.03133 13.00598
##  [73] 12.97313 12.93538 12.89534 12.85561 12.81879 12.78747 12.75809 12.72570
##  [81] 12.69090 12.65428 12.61642 12.57792 12.53934 12.50130 12.46436 12.42912
##  [89] 12.39617 12.36609 12.33948 12.31691 12.29772 12.28071 12.26569 12.25245
##  [97] 12.24078 12.23050 12.22138 12.21323 12.20586 12.19904 12.19259 12.18630
## [105] 12.17997 12.17339 12.16717 12.16200 12.15782 12.15455 12.15212 12.15045
## [113] 12.14948 12.14914 12.14934 12.15003 12.15113 12.15256 12.15426 12.15615
## [121] 12.15852 12.16164 12.16543 12.16980 12.17465 12.17991 12.18548 12.19128
## [129] 12.19721 12.20320 12.20915 12.21497 12.22338 12.23598 12.25109 12.26698
## [137] 12.28197 12.29434 12.30239 12.30614 12.30700 12.30541 12.30180 12.29660
## [145] 12.29023 12.28312 12.27571 12.26842 12.26167 12.25591 12.25155 12.24902
## [153] 12.24876 12.25120 12.25675 12.26411 12.27177 12.27985 12.28846 12.29774
## [161] 12.30780 12.31877 12.33077 12.34393 12.35836 12.37419 12.39155 12.41055
## [169] 12.43131 12.45634 12.48753 12.52416 12.56548 12.61077 12.65927 12.71027
## [177] 12.76301 12.81676 12.87079 12.92436 12.97673 13.02717 13.07493 13.11928
## [185] 13.15948 13.19481 13.22451 13.24785 13.26410 13.27252 13.27325 13.26736
## [193] 13.25549 13.23830 13.21643 13.19054 13.16126 13.12925 13.09516 13.05963
## [201] 13.02331 12.98685 12.95090 12.91611 12.87979 12.83926 12.79516 12.74816
## [209] 12.69890 12.64803 12.59621 12.54410 12.49233 12.44156 12.39245 12.34564
## [217] 12.30180 12.26156 12.22335 12.18520 12.14717 12.10932 12.07171 12.03442
## [225] 11.99750 11.96101 11.92502 11.88960 11.85480 11.82069 11.78734 11.75480
## [233] 11.72297 11.69168 11.66095 11.63077 11.60114 11.57206 11.54353 11.51555
## [241] 11.48812 11.46124 11.43490 11.40912 11.38388
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.3, n = 245)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.70096 10.79560 10.88827 10.97895 11.06765 11.15434 11.23902 11.32168
##   [9] 11.40231 11.48091 11.55746 11.63195 11.70438 11.77471 11.84294 11.90911
##  [17] 11.97325 12.03539 12.09557 12.15383 12.20993 12.26368 12.31514 12.36439
##  [25] 12.41150 12.45654 12.49959 12.54072 12.58001 12.61752 12.65334 12.68752
##  [33] 12.72015 12.75131 12.78025 12.80636 12.82984 12.85090 12.86974 12.88658
##  [41] 12.90162 12.91508 12.92716 12.93807 12.94801 12.95721 12.96585 12.97417
##  [49] 12.98114 12.98582 12.98847 12.98936 12.98875 12.98692 12.98413 12.98065
##  [57] 12.97675 12.97269 12.96875 12.96520 12.96229 12.96031 12.95851 12.95603
##  [65] 12.95293 12.94927 12.94511 12.94051 12.93553 12.93024 12.92469 12.91981
##  [73] 12.91602 12.91258 12.90880 12.90395 12.89731 12.88817 12.87702 12.86488
##  [81] 12.85184 12.83795 12.82331 12.80798 12.79205 12.77559 12.75867 12.74137
##  [89] 12.72378 12.70595 12.68798 12.66994 12.65168 12.63298 12.61377 12.59399
##  [97] 12.57359 12.55251 12.53068 12.50805 12.48456 12.46015 12.43476 12.40833
## [105] 12.38081 12.35212 12.31983 12.28223 12.24037 12.19532 12.14813 12.09986
## [113] 12.05157 12.00431 11.95914 11.91713 11.87932 11.84678 11.82056 11.80172
## [121] 11.78759 11.77489 11.76382 11.75455 11.74726 11.74214 11.73937 11.73913
## [129] 11.74160 11.74697 11.75541 11.76712 11.78770 11.81992 11.85971 11.90296
## [137] 11.94561 11.98356 12.01272 12.03786 12.06617 12.09720 12.13051 12.16565
## [145] 12.20217 12.23964 12.27760 12.31560 12.35322 12.38999 12.42547 12.45922
## [153] 12.49080 12.51975 12.54563 12.56827 12.58805 12.60541 12.62076 12.63451
## [161] 12.64710 12.65894 12.67045 12.68205 12.69417 12.70722 12.72162 12.73779
## [169] 12.75616 12.77737 12.80155 12.82831 12.85727 12.88805 12.92027 12.95355
## [177] 12.98752 13.02178 13.05596 13.08968 13.12256 13.15422 13.18427 13.21235
## [185] 13.23806 13.26102 13.28086 13.29720 13.30965 13.31784 13.32254 13.32483
## [193] 13.32481 13.32256 13.31818 13.31175 13.30336 13.29310 13.28107 13.26734
## [201] 13.25200 13.23516 13.21689 13.19728 13.17627 13.15373 13.12966 13.10406
## [209] 13.07694 13.04831 13.01816 12.98650 12.95333 12.91867 12.88250 12.84484
## [217] 12.80569 12.76505 12.72297 12.67946 12.63450 12.58804 12.54007 12.49054
## [225] 12.43942 12.38668 12.33229 12.27621 12.21841 12.15886 12.09752 12.03437
## [233] 11.96947 11.90291 11.83470 11.76484 11.69333 11.62018 11.54538 11.46893
## [241] 11.39085 11.31112 11.22976 11.14676 11.06213
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.3, n = 245)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.80045 10.86902 10.93619 11.00195 11.06625 11.12907 11.19038 11.25016
##   [9] 11.30836 11.36498 11.41997 11.47330 11.52495 11.57490 11.62318 11.66991
##  [17] 11.71515 11.75901 11.80157 11.84292 11.88225 11.91885 11.95297 11.98485
##  [25] 12.01475 12.04292 12.06960 12.09504 12.11949 12.14320 12.16643 12.18941
##  [33] 12.21239 12.23563 12.25908 12.28245 12.30568 12.32868 12.35137 12.37368
##  [41] 12.39552 12.41682 12.43750 12.45747 12.47666 12.49500 12.51239 12.52877
##  [49] 12.54464 12.56047 12.57606 12.59122 12.60577 12.61953 12.63230 12.64390
##  [57] 12.65414 12.66284 12.66981 12.67485 12.67780 12.67845 12.67682 12.67325
##  [65] 12.66803 12.66144 12.65377 12.64530 12.63634 12.62715 12.61803 12.60713
##  [73] 12.59301 12.57658 12.55876 12.54046 12.52259 12.50607 12.48900 12.46920
##  [81] 12.44707 12.42306 12.39759 12.37108 12.34398 12.31670 12.28968 12.26334
##  [89] 12.23811 12.21443 12.19271 12.17340 12.15559 12.13817 12.12119 12.10471
##  [97] 12.08880 12.07353 12.05895 12.04512 12.03211 12.01998 12.00880 11.99862
## [105] 11.98951 11.98153 11.97543 11.97168 11.96992 11.96980 11.97097 11.97307
## [113] 11.97576 11.97868 11.98147 11.98379 11.98528 11.98558 11.98435 11.98124
## [121] 11.97673 11.97164 11.96612 11.96031 11.95435 11.94838 11.94254 11.93698
## [129] 11.93183 11.92724 11.92336 11.92031 11.91730 11.91352 11.90909 11.90412
## [137] 11.89873 11.89304 11.88716 11.88003 11.87079 11.85988 11.84775 11.83482
## [145] 11.82154 11.80834 11.79567 11.78396 11.77364 11.76516 11.75896 11.75547
## [153] 11.75513 11.75838 11.76566 11.77611 11.78851 11.80273 11.81864 11.83612
## [161] 11.85504 11.87528 11.89669 11.91917 11.94258 11.96679 11.99168 12.01712
## [169] 12.04298 12.07188 12.10613 12.14511 12.18823 12.23488 12.28445 12.33635
## [177] 12.38997 12.44470 12.49994 12.55508 12.60953 12.66268 12.71392 12.76266
## [185] 12.80828 12.85018 12.88777 12.92042 12.94755 12.96855 12.98442 12.99670
## [193] 13.00557 13.01123 13.01388 13.01368 13.01085 13.00557 12.99803 12.98841
## [201] 12.97692 12.96373 12.94905 12.93305 12.91424 12.89127 12.86463 12.83477
## [209] 12.80219 12.76734 12.73071 12.69275 12.65395 12.61477 12.57569 12.53718
## [217] 12.49971 12.46375 12.42798 12.39084 12.35246 12.31298 12.27254 12.23127
## [225] 12.18930 12.14677 12.10382 12.06057 12.01717 11.97375 11.93045 11.88739
## [233] 11.84433 11.80094 11.75721 11.71314 11.66873 11.62398 11.57888 11.53343
## [241] 11.48763 11.44148 11.39498 11.34812 11.30090
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")